class Extensions::AirController < ApplicationController

  ###########################################################
  # メソッド：air_loginアクション
  # 概　　要：Airアプリからのログイン処理
  ###########################################################
  def login
    case request.method
      when :post
        if user = MstUser.authenticate(params['user_login_id'], params['user_password'])
          #ログイン日時更新
          user.update_attribute(:last_login_on, Time.now)
          item_list = {}
          item_list[:user_login_id] = params['user_login_id']
          item_list[:user_password] = params['user_password']
          item_list[:id] = user.id
          result = {
            :items => item_list
          }
          render :text => result_for_json( true, '', result )
      return
        else
          result = {
            :items => ""
          }
          render :text => result_for_json( false, '', result )
          return
      end
      result = {
        :items => ""
      }
      render :text => result_for_json( false, '', result )
      return
    else
      render :text => 'access denied!'
    end
  end


  #
  #--
  #Airアプリにより呼び出される各データの初期化データ
  #++
  #

  #
  #=== json_init_usersアクション
  #
  #mst_users情報をJSON形式で返す。
  #Airアプリから呼び出される
  #
  def json_init_users
    users = MstUser.find(:all, :conditions=>[" valid_flg=? ", 1])
    item_list = []
    users.each do | user |
         item_list.push(user.attributes)
    end

    result = {
      :items => item_list
    }

    render :text => result_for_json( true, '', result )
  end

  ###########################################################
  # メソッド：json_init_projectsアクション
  # 概　　要：dat_projects情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_projects
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :include    => [{:dat_projectusers=>:mst_user}],
      :order      => "dat_projects.id"
    }
    projects = DatProject.find(:all, opt)
    item_list = []
    projects.each do | project |
         item_list.push(project.attributes)
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json(true, '', result)
  end


  ###########################################################
  # メソッド：json_init_project_usersアクション
  # 概　　要：dat_project_users情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_project_users
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_projectcomps.line_no asc",
      :include    => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_milestone,
          {:dat_task => [{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event, :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do |project|
      items = project.dat_projectusers.map do |pu|
        {
          :id              => pu.id,
          :project_id      => pu.project_id,
          :email           => pu.email,
          :user_id         => pu.user_id,
          :create_user_id  => pu.create_user_id,
          :created_on      => pu.created_on,
          :active_flg      => pu.active_flg
        }
      end
      item_list.concat items
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json(true, '', result)
  end




  ###########################################################
  # メソッド：json_init_project_compsアクション
  # 概　　要：dat_project_comps情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_project_comps
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_projectcomps.line_no asc",
      :include    => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_milestone,
          {:dat_task=>[{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event,
          :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do | project |
      items = project.dat_projectcomps.map do |pc|
        {
          :id                    => pc.id,
          :project_id            => pc.project_id,
          :line_no               => pc.line_no,
          :task_kbn              => pc.task_kbn,
            :item_name           => pc.item_name,
            :item_name           => pc.item_name,
            :class_word1         => pc.class_word1,
            :class_word2         => pc.class_word2,
            :class_word3         => pc.class_word3,
            :create_user_id      => pc.create_user_id,
            :created_on          => pc.created_on,
            :update_user_id      => pc.update_user_id,
            :updated_on          => pc.updated_on,
            :last_operation_kbn  => pc.last_operation_kbn
        }
      end
      item_list.concat items
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json(true, '', result)
  end



  ###########################################################
  # メソッド：json_init_project_logsアクション
  # 概　　要：dat_projectlogs情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_project_logs
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_projectcomps.line_no asc",
      :include    => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_projectlogs,
          :dat_milestone,
          {:dat_task => [{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event,
          :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do | project |
      project.dat_projectcomps.each do |pc|
        items = pc.dat_projectlogs.map do |pl|
          {
            :id             => pl.id,
            :projectcomp_id => pl.projectcomp_id,
            :log_kbn        => pl.log_kbn,
            :create_user_id => pl.create_user_id,
            :created_on     => pl.created_on
          }
        end
        item_list.concat items
      end
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json(true, '', result)
  end



  ###########################################################
  # メソッド：json_init_project_logsアクション
  # 概　　要：dat_projectlogs情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_tasks
    dumy = params[:user_id]
    opt = {
        :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order        => "dat_projectcomps.line_no asc",
      :include      => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_projectlogs,
          :dat_milestone,
          {:dat_task => [{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event,
          :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do | project |
      project.dat_projectcomps.each do |pc|
        if ! pc.dat_task.blank?
            item_list.push pc.dat_task.attributes
        end
      end
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json(true, '', result)
  end



  ###########################################################
  # メソッド：json_init_taskusersアクション
  # 概　　要：dat_taskusers情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_taskusers
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_taskusers.id asc",
      :include    => [{:dat_projectusers=>[:dat_taskusers, :mst_user]}]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do | project |
      project.dat_projectusers.each do | pu |
        items = pu.dat_taskusers.map do |tu|
          {
            :id             => tu.id,
            :task_id        => tu.task_id,
            :projectuser_id => tu.projectuser_id,
            :create_user_id => tu.create_user_id,
            :created_on     => tu.created_on
          }
        end
        item_list.concat items
      end
    end
    result = {
      :items => item_list
    }
    render :text => result_for_json( true, '', result )
  end


  ###########################################################
  # メソッド：json_init_milestonesアクション
# 概　　要：dat_milestones情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_milestones
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_projectcomps.line_no asc",
      :include    => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_projectlogs,
          :dat_milestone,
          {:dat_task => [{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event,
          :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)
    item_list = []
    projects.each do | project |
      project.dat_projectcomps.each do |pc|
        if ! pc.dat_milestone.blank?
            item_list.push pc.dat_milestone.attributes
        end
      end
    end
    result = {:items => item_list}
    render :text => result_for_json( true, '', result )
  end



  ###########################################################
  # メソッド：json_init_eventsアクション
  # 概　　要：dat_events情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_events
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_projectcomps.line_no asc",
      :include    => [
        {:dat_projectusers => :mst_user},
        {:dat_projectcomps => [
          :dat_projectlogs,
          :dat_milestone,
          {:dat_task => [{:dat_user_main=>:mst_user}, {:dat_user_client=>:mst_user}]},
          :dat_event,
          :mst_user_create
        ]}
      ]
    }
    projects = DatProject.find(:all, opt)

    item_list = []
    projects.each do | project |
      project.dat_projectcomps.each do |pc|
        if ! pc.dat_event.blank?
            item_list.push pc.dat_event.attributes
        end
      end
    end
    result = {:items => item_list}
    render :text => result_for_json( true, '', result )
  end



  ###########################################################
  # メソッド：json_init_eventusersアクション
  # 概　　要：dat_eventusers情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_eventusers
    dumy = params[:user_id]
    opt = {
      :conditions => ["dat_projects.valid_flg = 1 AND dat_projects.id in (SELECT pu.project_id FROM dat_projectusers pu WHERE pu.user_id = ? ) ", dumy],
      :order      => "dat_eventusers.id asc",
      :include    => [{:dat_projectusers => [:dat_eventusers, :mst_user]}]
    }
    projects = DatProject.find(:all, opt)
    item_list = []
    projects.each do | project |
      project.dat_projectusers.each do | pu |
        items = pu.dat_eventusers.map do |tu|
        {
          :id             => tu.id,
          :event_id       => tu.event_id,
          :projectuser_id => tu.projectuser_id,
          :entry_flg      => tu.entry_flg,
          :create_user_id => tu.create_user_id,
          :created_on     => tu.created_on
        }
        end
        item_list.concat items
      end
    end

    result = {:items => item_list}
    render :text => result_for_json(true, '', result)
  end




  ###########################################################
  # メソッド：json_init_messagesアクション
  # 概　　要：mst_messages情報をJSON形式で返す。
  #           Airアプリから呼び出される
  ###########################################################
  def json_init_messages
    eventusers = MstMessage.find(:all, :order=>"id")
    item_list = []
    eventusers.each do | eventuser |
      item_list.push(eventuser.attributes)
    end
    result = {:items => item_list}
    render :text => result_for_json(true, '', result)
  end
end
